/*file chiron src/styleTest.js */
/*preamble

    Copyright (c) 2002-2008 Kris Kowal <http://cixar.com/~kris.kowal>
    MIT License
    
    The license terms are stated in full in <license.rst> and at the end
    of all source files.

*/

/*

 * testUserDevice.js 1.2
 * Authored by Scott Jehl, Filament Group (filamentgroup.com)
 * Date: July 2007 (rev. Feb 2008)
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
 * Notes: This script is used to test the capability of a browser before providing enhanced javascript and css functionality.
          The script waits until the document.body is ready and then performs the test. Upon passing, any arguments will be passed on to the domReady function (jQuery's if available). This allows you to enclose your existing domReady scripting in the test function call.
          
          Upon successful completion of the test, the following steps occur to allow for easy separation of enhanced style:
               1. a class of "enhanced" is assigned to the body element to be used for optional css scoping (such as: body.enhanced {background: red;})
               2. Any links to alternate stylesheets that have a class of "enhanced" will be enabled.
               3. Any links to stylesheets that have a class of "basicNoCascade" will be disabled. This is optional depending on whether your enhanced stylesheets cascade or conflict with your basic sheets.
          A cookie is set as well to notify the script on future page loads that the browser has already passed and can receive an enhanced experience. 
          * the cookie is (enhanced = true) and can be used on the backend to speed things up even further!
 * Usage: To use this script, simply enclose any scripting within the following function:
                                 enhancedDomReady(function(){
                                     //put scripts here to be executed after the dom is ready and the browser has passed the test
                                 });

 * Changes: 1.2: class and cookie name has been changed to "enhanced". This may break backward compatibility with older implementations of this test.
 * Additional Info: For more information, visit filamentgroup.com/lab/browserTest/

*/

include('cookie.js');

/*** test
*/
exports.test = function () {
    if (cookie.has(moduleUrl))
        return cookie.get(moduleUrl);

    var value = false;
    if (document.getElementById && document.createElement) {
        var newDiv = document.createElement('div');
        document.body.appendChild(newDiv);
        newDiv.style.visibility = 'hidden';
        newDiv.style.width = '20px';
        newDiv.style.padding = '10px';
        var divWidth = newDiv.offsetWidth;
        if(divWidth != 40) {document.body.removeChild(newDiv); return false;}
        newDiv.style.position = 'absolute';
        newDiv.style.left = '10px';
        var leftVal = newDiv.offsetLeft;
        if(leftVal != 10) { document.body.removeChild(newDiv); return false;}
        var newInnerDiv = document.createElement('div');
        newInnerDiv.style.width = '5px';
        newInnerDiv.style.cssFloat = 'left';
        newInnerDiv.style.styleFloat = 'left';
        newDiv.appendChild(newInnerDiv);
        var secondInnerDiv = newInnerDiv.cloneNode(true); 
        newDiv.appendChild(secondInnerDiv);
        var newInnerDivTop = newInnerDiv.offsetTop;
        var secondInnerDivTop = secondInnerDiv.offsetTop;
        if(newInnerDivTop != secondInnerDivTop) { document.body.removeChild(newDiv); return false;}
        newDiv.innerHTML = '<ul><li style="width: 5px; float: left;">test</li><li style="width: 5px; float: left;clear: left;">test</li></ul>';
        var top1 = newDiv.getElementsByTagName('li')[0].offsetTop;
        var top2 = newDiv.getElementsByTagName('li')[1].offsetTop;
        if(top1 == top2){return false;}
        newDiv.innerHTML = '<div style="height: 20px;"></div>';
        newDiv.style.padding = '0';
        newDiv.style.height = '10px';
        newDiv.style.overflow = 'auto';
        var newDivHeight = newDiv.offsetHeight;
        if(newDivHeight != 10){document.body.removeChild(newDiv); return false;}
        newDiv.innerHTML = '<div style="line-height: 2; font-size: 10px;">Te<br />st</div>';
        newDiv.style.padding = '0';
        newDiv.style.height = 'auto';
        newDiv.style.overflow = '';
        var newDivHeight = newDiv.offsetHeight;
        if(newDivHeight > 40){document.body.removeChild(newDiv); return false;}
        if(window.onresize == false){document.body.removeChild(newDiv); return false;}
        if(!window.print){ document.body.removeChild(newDiv); return false;}
        if(window.clientInformation && window.opera){document.body.removeChild(newDiv); return false;}
        document.body.removeChild(newDiv);
        enhanceDocument();
        createCookie('enhanced', 'true');
        value = true;
    }

    cookie.set(moduleUrl, value);
    return value;
};

/*** enhance
*/
//this gets called after test is passed
exports.enhance = function () {
    //add class to body "hasJS" for css scoping
    document.body.className += ' enhanced';
    //function to enable enhanced stylesheets (enables alt stylesheet links with class name "enhanced)
    var allLinks = document.getElementsByTagName('link');
    for(i=0; i<allLinks.length; i++){
        //if the link has a class of "basicNoCascade", disable it
        if (/\bbasicNoCascade\b/.exec(allLinks[i].className)){
            allLinks[i].disabled = true;
        }
        //if the link has a class of "enhanced", enable it
        if (/\benhanced\b/.exec(allLinks[i].className)){
            allLinks[i].disabled = true; //opera likes to have it toggled
            allLinks[i].disabled = false;
        }
    }
};


/*license

    Legal
    =======
    
    Chiron is a component of the Tale web-game project.
    
    See <credit.txt> for a complete list of
    contributions and their licenses.  All contributions are provided
    under permissive, non-viral licenses including MIT, BSD, Creative Commons
    Attribution 2.5, Public Domain, or Unrestricted.
    
    
    License
    =======
    
    Copyright (c) 2002-2008 Kris Kowal <http://cixar.com/~kris.kowal>
    MIT License
    
    
    MIT License
    -----------
    
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:
    
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

*/

